home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / bashsrc.zoo / mailcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-05  |  9.7 KB  |  367 lines

  1. /* mailcheck.c The thing that checks the mail files... */
  2.  
  3. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/param.h>
  25. #include "shell.h"
  26.  
  27. #ifndef MAXPATHLEN
  28. #define MAXPATHLEN 1024
  29. #endif
  30.  
  31. #define NOW ((time_t)time ((time_t *)0))
  32.  
  33. typedef struct {
  34.   char *name;
  35.   time_t access_time;
  36.   time_t mod_time;
  37. } FILEINFO;
  38.  
  39. /* The list of remembered mail files. */
  40. FILEINFO **mailfiles = (FILEINFO **)NULL;
  41.  
  42. /* Number of mail files that we have. */
  43. int mailfiles_count = 0;
  44.  
  45. /* The last known time that mail was checked. */
  46. int last_time_mail_checked = 0;
  47.  
  48. /* Returns non-zero if it is time to check mail. */
  49. time_to_check_mail ()
  50. {
  51.   char *temp = get_string_value ("MAILCHECK");
  52.   time_t now = NOW;
  53.   unsigned seconds = 0;
  54.  
  55.   if ((!temp || sscanf (temp, "%u", &seconds) == 0) ||
  56.       ((now - last_time_mail_checked < seconds)))
  57.     return (0);
  58.  
  59.   return (1);
  60. }
  61.  
  62. /* Okay, we have checked the mail.  Perhaps I should make this function
  63.    go away. */
  64. reset_mail_timer ()
  65. {
  66.   last_time_mail_checked = NOW;
  67. }
  68.  
  69. /* Locate a file in the list.  Return index of
  70.    entry, or -1 if not found. */
  71. find_mail_file (file)
  72.      char *file;
  73. {
  74.   register int index = 0;
  75.  
  76.   while (index < mailfiles_count)
  77.     if (strcmp ((mailfiles[index])->name, file) == 0) return index;
  78.     else index++;
  79.   return (-1);
  80. }
  81.  
  82. /* Add this file to the list of remembered files. */
  83. add_mail_file (file)
  84.      char *file;
  85. {
  86.   struct stat finfo;
  87.   char *full_pathname ();
  88.   char *filename = full_pathname (file);
  89.   int index = find_mail_file (file);
  90.  
  91.   if (index > -1)
  92.     {
  93.       if (stat (filename, &finfo) == 0)
  94.     {
  95.       mailfiles[index]->mod_time = finfo.st_mtime;
  96.       mailfiles[index]->access_time = finfo.st_atime;
  97.     }
  98.       free (filename);
  99.       return;
  100.     }
  101.  
  102.   if (!mailfiles)
  103.     mailfiles = 
  104.       (FILEINFO **)xmalloc ((mailfiles_count = 1) * sizeof (FILEINFO *));
  105.   else
  106.     mailfiles = 
  107.       (FILEINFO **)xrealloc (mailfiles,
  108.                  ((++mailfiles_count) * sizeof (FILEINFO *)));
  109.  
  110.   mailfiles[mailfiles_count - 1] = (FILEINFO *)xmalloc (sizeof (FILEINFO));
  111.   mailfiles[mailfiles_count - 1]->name = filename;
  112.   if (stat (filename, &finfo) == 0)
  113.     {
  114.       mailfiles[mailfiles_count - 1]->access_time = finfo.st_atime;
  115.       mailfiles[mailfiles_count - 1]->mod_time = finfo.st_mtime;
  116.     }
  117.   else
  118.     {
  119.       mailfiles[mailfiles_count - 1]->access_time
  120.     = mailfiles[mailfiles_count - 1]->mod_time = (time_t)-1;
  121.     }
  122. }
  123.  
  124. /* Reset the existing mail files access and modification times to zero. */
  125. reset_mail_files ()
  126. {
  127.   register int i;
  128.  
  129.   for (i = 0; i < mailfiles_count; i++)
  130.     mailfiles[i]->access_time = mailfiles[i]->mod_time = 0;
  131. }
  132.  
  133. /* Free the information that we have about the remembered mail files. */
  134. free_mail_files ()
  135. {
  136.   while (mailfiles_count--)
  137.     {
  138.       free (mailfiles[mailfiles_count]->name);
  139.       free (mailfiles[mailfiles_count]);
  140.     }
  141.   mailfiles_count = 0;
  142.   free (mailfiles);
  143.   mailfiles = (FILEINFO **)NULL;
  144. }
  145.  
  146. /* Return the full pathname of FILE.  Easy.  Filenames that begin
  147.    with a '/' are returned as themselves.  Other filenames have
  148.    the current working directory prepended.  A new string is
  149.    returned in either case. */
  150. char *
  151. full_pathname (file)
  152.      char *file;
  153. {
  154.   char *tilde_expand (), *disposer;
  155.   extern int disallow_filename_globbing;
  156.  
  157.   if (!disallow_filename_globbing && *file == '~')
  158.     file = tilde_expand (file);
  159.   else
  160.     file = savestring (file);
  161.  
  162.   if (absolute_pathname (file))
  163.     if (*file == '/')
  164.       return (file);
  165.  
  166.   disposer = file;
  167.  
  168.   {
  169.     char *current_dir = (char *)xmalloc (2 + MAXPATHLEN + strlen (file));
  170.     if (!getwd (current_dir))
  171.       {
  172.     report_error (current_dir);
  173.     free (current_dir);
  174.     return ((char *)NULL);
  175.       }
  176.     strcat (current_dir, "/");
  177.  
  178.     /* Turn /foo/./bar into /foo/bar. */
  179.     if (strncmp (file, "./", 2) == 0)
  180.       file += 2;
  181.  
  182.     strcat (current_dir, file);
  183.     free (disposer);
  184.     return (current_dir);
  185.   }
  186. }
  187.  
  188. /* Return non-zero if FILE's mod date has changed. */
  189. file_mod_date_changed (file)
  190.      char *file;
  191. {
  192.   time_t time = (time_t)NULL;
  193.   struct stat finfo;
  194.  
  195.   int index = find_mail_file (file);
  196.   if (index != -1)
  197.     time = mailfiles[index]->mod_time;
  198.  
  199.   if (stat (file, &finfo) == 0)
  200.     {
  201.       if (finfo.st_size != 0)
  202.     return (time != finfo.st_mtime);
  203.     }
  204.   return (0);
  205. }
  206.  
  207. /* Return non-zero if FILE's access date has changed. */
  208. file_access_date_changed (file)
  209.      char *file;
  210. {
  211.   time_t time = (time_t)NULL;
  212.   struct stat finfo;
  213.  
  214.   int index = find_mail_file (file);
  215.   if (index != -1)
  216.     time = mailfiles[index]->access_time;
  217.  
  218.   if (stat (file, &finfo) == 0)
  219.     {
  220.       if (finfo.st_size != 0)
  221.     return (time != finfo.st_atime);
  222.     }
  223.   return (0);
  224. }
  225.  
  226. #ifdef SYSV
  227. #define DEFAULT_MAIL_PATH "/usr/mail/"
  228. #else
  229. #define DEFAULT_MAIL_PATH "/usr/spool/mail/"
  230. #endif
  231.  
  232. /* Return the colon separated list of pathnames to check for mail. */
  233. char *
  234. get_mailpaths ()
  235. {
  236.   extern char *current_user_name;
  237.   char *mailpaths = get_string_value ("MAILPATH");
  238.   if (!mailpaths) mailpaths = get_string_value ("MAIL");
  239.   if (!mailpaths)
  240.     {
  241.       mailpaths = (char *)alloca (1 + strlen (DEFAULT_MAIL_PATH)
  242.                   + strlen (current_user_name));
  243.       strcpy (mailpaths, DEFAULT_MAIL_PATH);
  244.       strcat (mailpaths, current_user_name);
  245.     }
  246.   return (savestring (mailpaths));
  247. }
  248.  
  249. /* Remember the dates of the files specified by MAILPATH, or if there is
  250.    no MAILPATH, by the file specified in MAIL.  If neither exists, use a
  251.    default value, which we randomly concoct from using Unix. */
  252. remember_mail_dates ()
  253. {
  254.   char *mailpaths = get_mailpaths ();
  255.   char *mailfile, *extract_colon_unit ();
  256.   int index = 0;
  257.   
  258.   while (mailfile = extract_colon_unit (mailpaths, &index))
  259.     {
  260.       register int i;
  261.       for (i = 0;
  262.        mailfile[i] && mailfile[i] != '?' && mailfile[i] != '%';
  263.        i++);
  264.       mailfile[i] = '\0';
  265.       add_mail_file (mailfile);
  266.     }
  267. }
  268.  
  269. /* check_mail () is useful for more than just checking mail.  Since it has
  270.    the paranoids dream ability of telling you when someone has read your
  271.    mail, it can just as easily be used to tell you when someones .profile
  272.    file has been read, thus letting one know when someone else has logged
  273.    in.  Pretty good, huh? */
  274.  
  275. /* Check for mail in some files.  If the modification date of any
  276.    of the files in MAILPATH has changed since we last did a
  277.    remember_mail_dates () then mention that the user has mail.
  278.    Special hack:  If the shell variable MAIL_WARNING is on and the
  279.    mail file has been accessed since the last time we remembered, then
  280.    the message "The mail in <mailfile> has been read" is printed. */
  281. check_mail ()
  282. {
  283.   char *extract_colon_unit ();
  284.   char *current_mail_file, *you_have_mail_message;
  285.   char *mailpaths = get_mailpaths ();
  286.   int index = 0;
  287.   register int string_index;
  288.  
  289.   while ((current_mail_file = extract_colon_unit (mailpaths, &index)))
  290.     {
  291.       char *t, *tilde_expand ();
  292.       int use_user_notification;
  293.  
  294.       if (!*current_mail_file)
  295.     {
  296.       free (current_mail_file);
  297.       continue;
  298.     }
  299.  
  300.       t = full_pathname (current_mail_file);
  301.       free (current_mail_file);
  302.       current_mail_file = t;
  303.  
  304.       use_user_notification = 0;
  305.       you_have_mail_message = "You have mail in $_";
  306.  
  307.       for (string_index = 0; current_mail_file[string_index]; string_index++)
  308.     if (current_mail_file[string_index] == '?'
  309.         || current_mail_file[string_index] == '%')
  310.       {
  311.         current_mail_file[string_index] = '\0';
  312.         you_have_mail_message = current_mail_file + string_index + 1;
  313.         use_user_notification++;
  314.         break;
  315.       }
  316.  
  317.       if (file_mod_date_changed (current_mail_file))
  318.     {
  319.       WORD_LIST *tlist, *expand_string ();
  320.       char *string_list ();
  321.       int i;
  322.       bind_variable ("_", current_mail_file);
  323.  
  324.       add_mail_file (current_mail_file);
  325.  
  326.       i = find_mail_file (current_mail_file);
  327.  
  328.       /* If the user has just run a program which manipulates the
  329.          mail file, then don't bother explaining that the mail
  330.          file has been manipulated. */
  331.       if (i != -1 &&
  332.           mailfiles[i]->access_time == mailfiles[i]->mod_time)
  333.         goto next_mail_file;
  334.  
  335.       if (!use_user_notification)
  336.         {
  337.           if (i != -1 &&
  338.           mailfiles[i]->access_time < mailfiles[i]->mod_time)
  339.         you_have_mail_message = "You have new mail in $_";
  340.         }
  341.  
  342.       if ((tlist = expand_string (you_have_mail_message)))
  343.         {
  344.           char *tem = string_list (tlist);
  345.           you_have_mail_message = (char *)alloca (1 + strlen (tem));
  346.           strcpy (you_have_mail_message, tem);
  347.           free (tem);
  348.         }
  349.       else
  350.         you_have_mail_message = "";
  351.  
  352.       printf ("%s\n", you_have_mail_message);
  353.       dispose_words (tlist);
  354.     }
  355.  
  356.       if (find_variable ("MAIL_WARNING")
  357.       && file_access_date_changed (current_mail_file))
  358.     {
  359.       add_mail_file (current_mail_file);
  360.       printf ("The mail in %s has been read!\n", current_mail_file);
  361.     }
  362.     next_mail_file:
  363.       free (current_mail_file);
  364.     }
  365.   free (mailpaths);
  366. }
  367.